package com.itheima.test03;

import java.io.*;
import java.net.Socket;

/*
客户端：发送数据，接受服务器反馈

 */
public class ClientDemo {

    public static void main(String[] args) throws IOException {
        //创建发送端对象
        Socket socket = new Socket("127.0.0.1", 8858);
        //获取输出流
        OutputStream os = socket.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
        writer.write("晚上好2");

        //所有的字符流 都需要刷新流
        
        writer.flush();
        //告诉服务端 我的数据写完了?
        //os.close();//不仅仅把输出流关了  还把 连接也断开了
        socket.shutdownOutput();
        //接受数据
        //获取输入流
        InputStream is = socket.getInputStream();
        //读数据
        //现有  字节流    想要 字符流
        //输入 InputStream   Reader
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String s;
        while ((s=reader.readLine())!=null){
            System.out.println(s);
        }
        //释放资源
       // reader.close();
        socket.close();


    }
}
